//http创建
const express = require('express')
const app = express()
// 允许所有的请求形式
app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
//处理post请求的参数
const bodyParse = require('body-parser')
// 解析 application/json
app.use(bodyParse.json())
// 解析 application/x-www-form-urlencoded
// app.use(bodyParse.urlencoded())

var server = app.listen(8081, 'localhost', () => {
  var host = server.address().address
  var port = server.address().port
  console.log("应用实例，访问地址为 http://%s:%s", host, port)
})
//连接数据库
const mongoose = require('mongoose')
const url = "mongodb://localhost:27017/foxanddog"
mongoose.connect(url).then(res => {
  res && console.log('连接成功')
}).catch(err => {
  err && console.log('连接失败')
})

//查model
function getModel(schemaName) {
  const mySchema = mongoose.Schema()
  return mongoose.model(schemaName, mySchema, `${schemaName}s`)
}

//获取下拉表
const selectModel = getModel('option')
//数据字典接口
app.get('/getDataDictionary', function (req, res) {
  selectModel.find({}).select(`name value`).exec((err, result) => {
    console.log(result)
    res.send(result)
  })
})
//下拉框接口
app.post('/getSelectOption', function (req, res) {
  console.log(req.body)
  selectModel.findOne(req.body).select('option').exec((err, result) => {
    console.log(result.toObject().option)
    res.send(result.toObject().option)
  })
})
// shema 相当于对表结构的定义
// model 定义表
// 其实，在MongoDB中不能称为表，应该为文档
const tableSchema = new mongoose.Schema({
  tableCode: String,
  tableName: String,
  tableSchemaData: Array
})
const tableModel = mongoose.model('tableSchema', tableSchema)
//保存表格结构表接口
app.post('/saveDragData', (req, res) => {
  console.log(req.body)
  tableModel.updateOne({tableCode: req.body.tableCode}, {$set: req.body}, {upsert: true}).exec((err, result) => {
    if (err) {
      console.log(err)
      res.send(err)
    } else {
      res.send(result)
    }
  })
})
//查询表名字典
app.get('/getFormName', (req, res) => {
  tableModel.find({}).select('tableName tableCode').exec((err, result) => {
    res.send(result)
  })
})
//查询表格结构接口
app.post('/getDragData', (req, res) => {
  tableModel.findOne(req.body).select('tableSchemaData').exec((err, result) => {
    console.log(result)
    res.send(result)
  })
})
// const db=mongoose.connection
// db.on('open',function (err) {
//   if (err){
//     console.log('数据库连接失败')
//     throw err
//   }
//   console.log('数据库连接成功')
// var catSchema=mongoose.Schema({name:String})
// catSchema.methods.speak=function (){
//   var greeting=this.name?`Meow name is ${this.name}`:`I don't have a name`
//   console.log(greeting)
// }
// var Catn=mongoose.model('Catn',catSchema)
// var kitty=new Catn({name:'kitty'})
// kitty.speak()
// kitty.save(function (err, kitty) {
//   if (err) return console.error(err);
//   kitty.speak();
// });
// var cc=new Catn({name:'cc'})
// cc.speak()
// cc.save(function (err,cc){
//   if (err) return console.error(err);
//   cc.speak();
// })
// Catn.find(function (err, kittens) {
//   if (err) return console.error(err);
//   console.log(kittens);
// })
// })
// app.get('/getDataDictionary',function (req,res) {
//   db.options.find({},{name:1,value:1})
// })
